package cn.gyxy.service.impl;

import cn.gyxy.Aspect.Bsquanxian;
import cn.gyxy.Aspect.Bsrizhi;
import cn.gyxy.mapper.GuanliMapper;
import cn.gyxy.mapper.LoginMapper;
import cn.gyxy.pojo.Guanli;
import cn.gyxy.service.GuanliService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GuanliServiceimpl implements GuanliService {
    @Autowired
    private GuanliMapper guanliMapper;

    @Autowired
    private LoginMapper login;


    @Override
    public List<Guanli> guanliChaxun() {
        List<Guanli> list = guanliMapper.guanliChaxun();
        for (Guanli l : list) {
            if (l.getPermission().equals(1)) {
                l.setPermission("超级管理员");
            } else {
                l.setPermission("普通管理员");
            }
        }
        return list;
    }

    @Bsrizhi(operation = "日志业务")
    @Override
    @Bsquanxian(operation = "权限判断")
    public void DeleteByIdguanli(Integer id) {
        //获取登陆用户
        Guanli guanli= (Guanli) SecurityUtils.getSubject().getPrincipal();
        //获取选中用户
         Guanli gl=guanliMapper.doIdchaxunGuanli(id);
        //判断如果删除的目标也是超级管理员抛出错误！
        if (gl.getPermission().equals(1))
            throw new AuthorizationException();
        if(guanli.getId().equals(gl.getId()))
            throw new IllegalArgumentException("不能删除自己");
        guanliMapper.DeleteByIdguanli(id);
    }

    @Bsrizhi(operation = "日志业务")
    @Override
    @Bsquanxian(operation = "权限判断")
//    @RequiresPermissions("1")
    public void guanliAdd(Guanli guanli) {
        Guanli gl = login.loginbs(guanli);
        if (guanli.getPermission().equals("超级管理员")) {//如果穿回来permission字段=超级管理员，设置成1状态
            guanli.setPermission(1);
        } else {
            guanli.setPermission(0);
        }
        if (gl != null)
            throw new IllegalArgumentException("该账号的管理员已存在");
        if (guanli.getPassword() == null || guanli.getPassword() == "")
            throw new IllegalArgumentException("密码不能为空");
        guanliMapper.guanliAdd(guanli);
    }

    @Bsrizhi(operation = "日志业务")
    @Override
    public void guanlimima(Guanli guanli) {
        Guanli gl = login.loginbs(guanli);
        if (gl == null)
            throw new IllegalArgumentException("不存在此账号的管理员");
        if (gl.getPassword().equals(guanli.getPassword()) == false)
            throw new IllegalArgumentException("原密码不正确");
        if (guanli.getNewpassword1() == null || guanli.getNewpassword1() == "" || guanli.getNewpassword2() == null || guanli.getNewpassword2() == "")
            throw new IllegalArgumentException("新密码不能为空");
        if (guanli.getNewpassword1().equals(guanli.getNewpassword2()) == false)
            throw new IllegalArgumentException("两次新密码不一致");
        if (gl.getPassword().equals(guanli.getNewpassword2()))
            throw new IllegalArgumentException("原密码不能与新密码相同");
        guanliMapper.guanlimima(guanli);


    }
}
